package freedom.ms.auth.controller;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import freedom.ms.auth.entity.Menu;
import freedom.ms.auth.service.MenuService;

/**
 * @author charlie
 *
 */
@Controller
public class HomeController {

	@Value("${spring.application.name}")
	String name;
	@Value("${server.port}")
	String port;

	@RequestMapping({"/inf"})
	@ResponseBody
	public String info() {
		String test = null;
		System.out.println(test.length());
		return MessageFormat.format("{0} {1}", name, port);
	}

	@RequestMapping({ "/index", "/" })
	public String index() {
		return "index";
	}

	public static final String LOGIN_MSG = ">>>>>>>>>>>login...error:{0}, logout:{1}";

	@GetMapping("/login")
	public String login(HttpServletRequest request, String error, String logout) {
		System.out.println(MessageFormat.format(LOGIN_MSG, error, logout));
		return "login";
	}

	@GetMapping("/admin")
	public String admin() {
		return "admin";
	}

	@GetMapping("/user")
	public String user() {
		return "user";
	}

	@GetMapping("/about")
	public String about() {
		return "about";
	}

	@GetMapping("/403")
	public String error403() {
		return "403";
	}
	
	@GetMapping("/menu-mgt")
	public String menuMgt() {
		return "menu-mgt";
	}
	
	@Autowired
	private MenuService menuService;
	
	@GetMapping("/menus")
	@ResponseBody
	public List<Menu> menus() {
		List<Menu> menus = menuService.findAll();
		return menus;
	}
	
	@GetMapping("/page")
	public String page()
	{
		return "page";
	}
	@GetMapping("/page/data")
	@ResponseBody
	public Map<String, Object> page(String sex, String search, String sort, String order, int offset, int limit)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		List<Student> list = new ArrayList<Student>();
		list.add(new Student("1", "2", "男", "1987-10-20", "8c1"));
		map.put("rows", list);
		map.put("total", 1);
		return map;
	}

}
